$(function () {
    $("#ip1").keyup(function () {
        // alert("你好")
        $('#msg').text('你好' + $('#ip1').val());
    });
    var lastNum = 0;
    var limit = 0;
    chrome.storage.sync.get('total', function ({ total }) {
        if (total) {
            lastNum = parseFloat(total);
        }
        $("#total").text(lastNum);
    })
    chrome.storage.sync.get("limit", function ({ limit }) {
        limit = parseFloat(limit)
        $('#limittotal').text(limit);
    })
    $("#btn").click(function () {
        curNum = parseFloat($('#inp2').val())
        // alert("val"+$('#inp2').val());
        if (curNum) {
            newTotal = curNum + lastNum;
            chrome.storage.sync.set({ 'total': newTotal }, function () {
                if (newTotal > limit) {
                    // alert("超出限制")
                    var options = {
                        type: 'basic',
                        title: '金额超出限制',
                        iconUrl: 'img/logo.png',
                        message: '您的消费金额超出限制'
                    }
                    chrome.notifications.create('limitnotify', options)
                }
            })
            lastNum = newTotal
        }
        $('#total').text(newTotal)
        $("#inp2").val('')

    })
    // 解除封印
    $("#btn1").click(() => {
        chrome.tabs.query({ active: true, currentWindow: true }, (tab) => {
            chrome.tabs.sendMessage(tab[0].id, { greeting: 'hello' }, (resp) => {
                const { code, message, result } = resp;
                // if (code === 1) {
                //     result.designMode = "on";
                // } else {
                //     alert(message)
                // }
            })
        })

    })
    // 学习
    $("#btn2").click(() => {
        chrome.tabs.query({ active: true, currentWindow: true }, (tab) => {
            chrome.tabs.sendMessage(tab[0].id, { greeting: 'study' }, (resp) => {
                const { code, message, result } = resp;

                if (code !== 1) {
                    alert(message)
                }
            })
        })
    })
})
